package com.mystnihon.tdroid.service;

import java.lang.ref.WeakReference;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.mystnihon.tdroid.manager.ftp.FTPManager;
import com.mystnihon.tdroid.manager.ftp.FTPManager.FTPListener;

public class FTPService extends Service implements IFTPService {

	private FTPManager mFTPManager;

	public FTPService() {
		mFTPManager = new FTPManager(this.getApplication());
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		return super.onStartCommand(intent, flags, startId);
	}

	public class BackgroundServiceBinder<S> extends Binder {
		// private final String TAG = BackgroundServiceBinder.class.getSimpleName();
		private WeakReference<S> mService;

		public BackgroundServiceBinder(S service) {
			mService = new WeakReference<S>(service);
		}

		public S getService() {
			return mService.get();
		}
	}

	// public class BackgroundServiceBinder extends Binder {
	// public FTPService getService() {
	// return FTPService.this;
	// }
	// }// ////

	@Override
	public IBinder onBind(Intent intent) {
		return new BackgroundServiceBinder<FTPService>(this);
	}

	public FTPManager getFTPManager() {
		return mFTPManager;
	}

	public void setFTPListener(FTPListener listener) {
		mFTPManager.setFTPListener(listener);
	}

	@Override
	public void onDestroy() {
		mFTPManager.clearListeners();
		mFTPManager.disconnect();
		mFTPManager = null;
		super.onDestroy();
	}

}
